<!-- demo.vue -->
<template>
  <div class="box-item">
    <li class="number-item" v-for="(item, index) in orderNum" :key="index">
      <span class="number-item-span">
        <i class="number-item-i" ref="numberItem">0123456789</i>
      </span>
    </li>
  </div>
</template>
  
<script>
export default {
  data() {
    return {
      orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'],
      count: 100
    }
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem // 拿到数字的ref，计算元素数量
      const numberArr = this.orderNum.filter(item => !isNaN(item))
      // console.log(22222, numberArr);
      // 结合CSS 对数字字符进行滚动,显示订单数量
      for (let index = 0; index < numberItems.length; index++) {
        const elem = numberItems[index]
        elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
      }
    },
    // 处理总订单数字
    toOrderNum(num) {
      num = num.toString()
      console.log(111, num);
      // 把订单数变成字符串
      if (num.length < 8) {
        num = '0' + num // 如未满八位数，添加"0"补位
        console.log(333333, num);
        this.toOrderNum(num) // 递归添加"0"补位
      } else if (num.length === 8) {
        // 订单数中加入逗号
        // num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
        this.orderNum = num.split('') // 将其便变成数据，渲染至滚动数组
      } else {
        // 订单总量数字超过八位显示异常
        this.$message.warning('订单总量数字过大，显示异常，请联系客服')
      }
    },
    increaseNumber() {
      // eslint-disable-next-line @typescript-eslint/no-this-alias
      let self = this
      this.timer = setInterval(() => {
        self.count = self.count + this.getRandomNumber(1, 100)
        this.toOrderNum(self.count)
        this.$nextTick(() => {
          self.setNumberTransform(self.count)
        })
      }, 3000)
    },
    getRandomNumber(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min)
      //随机生成
    }
  },
  mounted() {
    this.increaseNumber()
  }
}
</script>
  
<style lang=scss>
.number-item {
  width: 41px;
  height: 75px;
  list-style: none;
  margin-right: 5px;
  // background: rgb(11, 178, 219);
  border-radius: 4px;
  border: 2px solid rgb(53, 186, 226);
  display: inline-block;
  font-size: 60px;
  color: rgb(53, 186, 226);

}

.number-item-span {
  position: relative;
  display: inline-block;
  margin-right: 10px;
  width: 100%;
  height: 100%;
  writing-mode: vertical-rl;
  text-orientation: upright;
  overflow: hidden;
}

.number-item-i {
  font-style: normal;
  position: absolute;
  top: 7px;
  left: 50%;
  transform: translate(-50%, 0);
  transition: transform 1s ease-in-out;
  letter-spacing: 10px;
}

.number-item:last-child {
  margin-right: 0;
}

.comma {
  display: inline-block;
  color: aqua;
  font-size: 30px;
}
</style>
  
  